<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/21
 * Time: 11:45
 */
namespace Publish\Controller;
use Common\Controller\HomebaseController;
include '../Common/function.php';
class DetailsController extends HomebaseController{
	protected $terms_model;
	protected $image_model;
	protected $person_model;
	protected $history_model;
	protected $story_model;
	protected $information_model;
	protected $dictionary_model;
	protected $video_model;
	protected $appendix_model;
	protected $systemtermrelationships_model;
	function _initialize() {
		parent::_initialize();
		$this->terms_model=D("Publish/SystemTerms");
		$this->image_model=D("Publish/PubImage");
		$this->person_model = D('Publish/PubPerson');
		$this->history_model=D('Publish/PubHistory');
		$this->story_model=D('Publish/PubStory');
		$this->information_model=D('Publish/PubInformation');
		$this->dictionary_model=D('Publish/PubDictionary');
		$this->video_model=D('Publish/PubVideo');
		$this->appendix_model=D('Publish/PubAppendix');
		$this->term_relationships_model = D("Publish/SystemTermRelationships");
	}
	public function index(){
		/* 查出所有的一级分类*/
		$first=$this->terms_model
			->field('term_id,name,description')
			->where(array('parent'=>0,'status'=>1))
			->where(array('term_id'=>array('NEQ','72')))
			->order("listorder desc,term_id asc")
			->select();
		for($i=0;$i<count($first);$i++){
			$list=$this->terms_model
				->where(array('parent'=>$first[$i]['term_id'],'status'=>1))
				->field('name,term_id,description')
				->order("listorder desc,term_id asc")
				->select();
			if(count($list)>0){
				foreach($list as $k=>$v){
					$newarr[$i]['name']=$first[$i]['name'];
					$newarr[$i]['term_id']=$first[$i]['term_id'];
					$newarr[$i]['description']=$first[$i]['description'];
					$newarr[$i]['term_son']['term_id'][]=$v['term_id'];
					$newarr[$i]['term_son']['name'][]=$v['name'];
				}
			}else{
				$newarr[$i]=$first[$i];
			}
		}
//		查询所有 的附录
		$appendix_right=$this->appendix_model->order("id asc")->select();
		//获取搜索值
		$keyword=$_POST['keyword'];
		$this->assign('keyword',$keyword);
//		获取人物id
		$person_id=$_GET['person_id'];
		$newArr=$_GET['newArr'];
		$is_highsearch=$_GET['is_highsearch'];
		$this->assign("person_id",$person_id);
		$this->assign("newArr",$newArr);
        $this->assign("is_highsearch",$is_highsearch);
        $this->assign('first',$newarr);
		$this->assign('two',$first['asd']);
		$this->assign('appendix_right',$appendix_right);
		$aa=isMobile();
		if($aa){
			$this->display("details_m");
		}else{
			$this->display();
		}
	}
	//珍贵图片（大图）
    public function show(){
        $person_id = I("person_id"); //人物id
         // $person_id=60;
        $num=$_GET['img_num'];
        $img_list = $this->image_model
            ->where(array("person_id"=>$person_id,"img_mark"=>1))
            ->select();
        $this->assign('img_list',$img_list);
        $this->assign("nums",$num);
    	$aa=isMobile();
		if($aa){
			// dump($img_list);
			$this->display("imgList_m");
		}else{
			$this->display();
		}
    }
//    组织序列表图片
	public function shows(){
		$term_id = I("term_id");
		$term_info = $this->terms_model->find($term_id);
		$term_info['imgs'] = json_decode($term_info['imgs'],true);
        $this->assign('term',$term_info['imgs']);
		$aa=isMobile();
		if($aa){
			$this->display("imgList_m");
		}else{
			$this->display("Details/show");
		}
	}
	//将领相关视频
	public  function video(){
		$term_id = I("term_id"); //分类id
		$video_list=$this->video_model
			->field("person_id,v_url,v_name")
			->alias("v")
			->join("__SYSTEM_TERM_RELATIONSHIPS__ str ON v.person_id=str.object_id")
			->join("__SYSTEM_TERMS__ st ON str.term_id=st.term_id")
			->where(array("parent"=>$term_id))
			->select();
		for($i=0;$i<count($video_list);$i++){
			$p_name=$this->person_model
				->field("name")
				->where(array("id"=>$video_list[$i]['person_id']))
				->find();
			$video_list[$i]['name']=$p_name['name'];
		}
//		echo $this->ajaxReturn($video_list);
		echo json_encode($video_list,true);
	}
	public function first(){
		
		$id=$_GET['id'];
		// $id=60;
		// 一级菜单右侧文本内容
		$data=$this->terms_model
			->field('description,term_id')
			->where("term_id=$id")
			->find();
        $term_info = $this->terms_model->find($id);
        $term_info['imgs'] = json_decode($term_info['imgs'],true);
        if(!$term_info['imgs']){
            $data['has_img'] = 0;
        }
        $this->ajaxReturn($data,'json');
    }
	
	public function appendix(){
//		附录详情内容
		$id=$_POST['id'];
		$data=$this->appendix_model
			->where("id=$id")
			->find();
		$data['content']=htmlspecialchars_decode($data['content']);
		// $all_menu=
		$this->ajaxReturn($data,'json');
}
	public function twotwo(){
		$keyword=$_POST['keyword'];
		if(!empty($keyword)) {
			$p_list = $this->person_model
				->field("id,name")
				->where("concat(name) like '%$keyword%'")
				->order("listorder desc,id asc")
				->select();
			for ($i = 0; $i < count($p_list); $i++) {
				$p_img = $this->image_model
					->field("img_url")
					->where(array("img_mark" => 0, "person_id" => $p_list[$i]['id']))
					->select();
				$p_list[$i]['img_url'] = $p_img[0]['img_url'];
				$p_list[$i]['person_id'] = $p_list[$i]['id'];
                $p_list[$i]['is_search']=1;
            }
			echo json_encode($p_list);
		}
//		}else{
//			$id=$_GET['id'];
//			$data=$this->image_model
//				->alias("a")
//				->join("__SYSTEM_TERM_RELATIONSHIPS__ as b on b.object_id =a.person_id")
//				->join("__PUB_PERSON__ as c on b.object_id=c.id")
//				->field('a.person_id,a.img_url,c.name')
//				->where("b.term_id=$id")
//				->select();
//			echo json_encode($data);
//		}
	}
	public function two(){
		$id=$_GET['id'];
		$data=$this->person_model
			->alias("p")
			->join("__SYSTEM_TERM_RELATIONSHIPS__ as b on b.object_id =p.id")
			->field('p.id,p.name')
			->where(array("b.term_id"=>$id))
			->order("p.listorder desc,p.id asc")
			->select();
		for($i=0;$i<count($data);$i++){
			$imgs=$this->image_model
				->where(array("person_id"=>$data[$i]['id'],"img_mark"=>0))
				->find();
			$data[$i]['img_url']=$imgs['img_url'];
		}
		echo json_encode($data);
	}
	//上一个人物。下一个人物
	public function person_list(){
		$id=$_GET['id'];
		$data=$this->person_model
			->alias("p")
			->join("__SYSTEM_TERM_RELATIONSHIPS__ as b on b.object_id =p.id")
			->field('p.id')
			->where(array("b.term_id"=>$id))
			->order("p.id asc")
			->select();
		echo json_encode($data);
	}
	public function twot(){
		$id=$_GET['id'];
		$data=$this->terms_model
			->field('description,term_id')
			->where("term_id=$id")
			->find();
		$this->ajaxReturn($data,'json');
	}
	public function three(){
		// $aaa=$_GET['id'];
		// echo $aaa;

		// 词条替换
		$citiao=$this->dictionary_model->field('id,dicname,parameters,identification')->select();
		// echo "<pre>";
		// print_r($citiao);
		// exit;
		// 查询所有的一级类id
		$tone=$this->terms_model->field('term_id,parent')->where("parent=0")->select();
		$newone=array();
		foreach ($tone as $kone => $vone) {
		    $newone[]=$vone['term_id'];
		}
		
		// 查询所有的二级类id
		$ttwo=$this->terms_model->field('term_id,parent')->where("parent!=0")->select();
		$newtwo=array();
		foreach ($ttwo as $ktwo => $vtwo) {
		    $newtwo[]=$vtwo['term_id'];
		}
		// echo "<pre>";
		// print_r($newtwo);
		// exit;

		$person_id=$_GET['id'];

//		 $person_id=60;
		//生平简介
		$p_list=$this->person_model
			->where(array("id"=>$person_id))
			->order("id asc")
			->find();
		$p_list['abstract']=htmlspecialchars_decode($p_list['abstract']);
		$content=$p_list['abstract'];
		foreach ($citiao as $key =>$value){
			// 判断是人物还是分类
			if($value['identification']==0){
				// 是人物
				// 查询出现的第一次的位置
				$citiao[$key]['result'] = strpos($content,$value['dicname']);
				// 被替换成以下内容
				// 判断是手机则替换成a链接跳转页面
				$aa=isMobile();
				if($aa){
					$connect="index.php?g=&m=Details&a=three&id=$value[parameters]";
					// $connect="{:U('Publish/Details/three?id=$value[parameters]')}";
					// $connect=U("Details/three?id=".$value['parameters'];
					$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
					// echo $citiao[$key]['link_name'];
					// die;
				}else{
					$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='person_id' person_id=$value[parameters] onclick=openthree('',$value[parameters])>".$value['dicname']."</a>";
				}
				// $citiao[$key]['link_name'] = "<a href='".$value['url']."'>".$value['dicname']."</a>";

				$content = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$content,1);// 只匹配一次关键词
			}elseif($value['identification']==1){
				// 判断一级类还是二级类
                if(in_array($value['parameters'],$newtwo)){ 
                    // 属于二级类
                    $citiao[$key]['result'] = strpos($content,$value['dicname']);
                    // 判断是手机则替换成a链接跳转页面
					$aa=isMobile();
					if($aa){
						// $connect="{:U('Second/index?id=$value[parameters]')}";
						$connect="index.php?g=&m=Second&a=index&id=$value[parameters]";
						$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
					}else{
						$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='classtwo_id' classtwo_id=$value[parameters] onclick=opentwo('',$value[parameters])>".$value['dicname']."</a>";
					}
                    
                    $content = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$content,1);// 只匹配一次关键词
                }else{
                    // 属于一级类
                    $citiao[$key]['result'] = strpos($content,$value['dicname']);
                     // 判断是手机则替换成a链接跳转页面
					$aa=isMobile();
					if($aa){
						$connect="index.php?g=&m=First&a=index&id=$value[parameters]";
						$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
					}else{
						$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='classone_id' classone_id=$value[parameters] onclick=openone('',$value[parameters])>".$value['dicname']."</a>";
					}
                    
                    $content = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$content,1);// 只匹配一次关键词
                }
			}else{
//				这里是文章词条
				  // 判断是手机则替换成a链接跳转页面
					$aa=isMobile();
					if($aa){
						$connect="index.php?g=&m=Details&a=marticle&id=$value[id]";

						$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
					}else{
						$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='sid' sid='$value[id]' onclick=openarticle($value[id])>".$value['dicname']."</a>";
					}
				
				$content = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$content,1);// 只匹配一次关键词
			}

		}
		$p_list['abstract']=htmlspecialchars_decode($content);
		$str=$p_list['abstract'];
			$dbc = array(
				 '（' , '）'
			);
			$sbc = array( //半角
				 '(', ')'
			);
		$p_list['abstract']=str_replace( $sbc, $dbc, $str ); //半角到全角;
		//抗战历程
		$h_list=$this->history_model
			->where(array("person_id"=>$person_id))
			->find();
		$content=$h_list['content'];
		foreach ($citiao as $key =>$value){
			// 判断是人物还是分类
			if($value['identification']==0){
				// 查询出现的第一次的位置
				$citiao[$key]['result'] = strpos($content,$value['dicname']);
				// 被替换成以下内容
				// 判断是手机则替换成a链接跳转页面
				$aa=isMobile();
				if($aa){
					$connect="index.php?g=&m=Details&a=three&id=$value[parameters]";
					// $connect="{:U('Publish/Details/three?id=$value[parameters]')}";
					// $connect=U("Details/three?id=".$value['parameters'];
					$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
					// echo $citiao[$key]['link_name'];
					// die;
				}else{
					$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='person_id' person_id=$value[parameters] onclick=openthree('',$value[parameters])>".$value['dicname']."</a>";
				}

				$content = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$content,1);// 只匹配一次关键词
			}elseif($value['identification']==1){
				// 判断一级类还是二级类
                if(in_array($value['parameters'],$newtwo)){ 
                    // echo "<pre>";
                    // print_r("123");
                    // exit; 
                    // 属于二级类
                    $citiao[$key]['result'] = strpos($content,$value['dicname']);
                   // 判断是手机则替换成a链接跳转页面
					$aa=isMobile();
					if($aa){
						// $connect="{:U('Second/index?id=$value[parameters]')}";
						$connect="index.php?g=&m=Second&a=index&id=$value[parameters]";
						$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
					}else{
						$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='classtwo_id' classtwo_id=$value[parameters]    onclick=opentwo('',$value[parameters])>".$value['dicname']."</a>";
					}
                    $content = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$content,1);// 只匹配一次关键词
                }else{
                    // echo "<pre>";
                    // print_r("456");
                    // exit;
                    // 属于一级类
                    $citiao[$key]['result'] = strpos($content,$value['dicname']);
                    // 判断是手机则替换成a链接跳转页面
					$aa=isMobile();
					if($aa){
						$connect="index.php?g=&m=First&a=index&id=$value[parameters]";
						$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
					}else{
						$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='classone_id' classone_id=$value[parameters]  onclick=openone('',$value[parameters])>".$value['dicname']."</a>";
					}
                    $content = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$content,1);// 只匹配一次关键词
                }
			}else{
//				这里是文章词条
				// 判断是手机则替换成a链接跳转页面
					$aa=isMobile();
					if($aa){
						$connect="index.php?g=&m=Details&a=marticle&id=$value[id]";

						$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
					}else{
						$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='sid' sid='$value[id]' onclick='openarticle($value[id])'>".$value['dicname']."</a>";
					}
				$content = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$content,1);// 只匹配一次关键词
			}

		}

		$h_list['content']=htmlspecialchars_decode($content);
		$str=$h_list['content'];
		$dbc = array(
			'（' , '）'
		);
		$sbc = array( //半角
			'(', ')'
		);
		$h_list['content']=str_replace( $sbc, $dbc, $str ); //半角到全角;
		//生动故事
		$s_list=$this->story_model
			->where(array("person_id"=>$person_id))
			->select();
		$contents=array();
		foreach ($s_list as $key => $value) {
			$s_list[$key]['title']=htmlspecialchars_decode($s_list[$key]['title']);
					// $content[]=$s_list[$key]['content'];
					$contents[]=$value['content'];
			// $s_list[$key]['content']=htmlspecialchars_decode($content);
		}
		 $conumber=count($contents);
		foreach ($citiao as $key =>$value){
			// 判断是人物还是分类
			if($value['identification']==0){
				// 查询出现的第一次的位置
				for ($i=0; $i < $conumber; $i++) { 
					$citiao[$key]['result'] = strpos($contents[$i],$value['dicname']);
					// 被替换成以下内容
					// 判断是手机则替换成a链接跳转页面
					$aa=isMobile();
					if($aa){
						$connect="index.php?g=&m=Details&a=three&id=$value[parameters]";
						// $connect="{:U('Publish/Details/three?id=$value[parameters]')}";
						// $connect=U("Details/three?id=".$value['parameters'];
						$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
						// echo $citiao[$key]['link_name'];
						// die;
					}else{
						$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='person_id' person_id=$value[parameters] onclick=openthree('',$value[parameters])>".$value['dicname']."</a>";
					}
					$contents[$i] = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$contents[$i],1);// 只匹配一次关键词
				}
			}elseif($value['identification']==1){
				// 判断一级类还是二级类
                if(in_array($value['parameters'],$newtwo)){ 
                    // 属于二级类
                    for ($i=0; $i < $conumber; $i++) { 
	                    $citiao[$key]['result'] = strpos($contents[$i],$value['dicname']);
	                    // 判断是手机则替换成a链接跳转页面
						$aa=isMobile();
						if($aa){
							// $connect="{:U('Second/index?id=$value[parameters]')}";
							$connect="index.php?g=&m=Second&a=index&id=$value[parameters]";
							$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
						}else{
							$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='classtwo_id' classtwo_id=$value[parameters] onclick=opentwo('',$value[parameters])>".$value['dicname']."</a>";
						}
	                    $contents[$i] = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$contents[$i],1);// 只匹配一次关键词
                    }
                }else{
                    // 属于一级类
                    for ($i=0; $i < $conumber; $i++) {
	                    $citiao[$key]['result'] = strpos($contents[$i],$value['dicname']);
	                     // 判断是手机则替换成a链接跳转页面
						$aa=isMobile();
						if($aa){
							$connect="index.php?g=&m=First&a=index&id=$value[parameters]";
							$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
						}else{
							$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='classone_id' classone_id=$value[parameters] onclick=openone('',$value[parameters])>".$value['dicname']."</a>";
						}
	                    $contents[$i] = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$contents[$i],1);// 只匹配一次关键词
	                }
                }
			}else{
//				这里是文章词条
				for ($i=0; $i < $conumber; $i++) {
					$citiao[$key]['result'] = strpos($contents[$i],$value['dicname']);
					// 判断是手机则替换成a链接跳转页面
					$aa=isMobile();
					if($aa){
						$connect="index.php?g=&m=Details&a=marticle&id=$value[id]";

						$citiao[$key]['link_name'] = "<a href=".$connect.">".$value['dicname']."</a>";
					}else{
						$citiao[$key]['link_name'] = "<a href='#' class='changes' bname='sid' sid='$value[id]' onclick=openarticle($value[id])>".$value['dicname']."</a>";
					}
					$contents[$i] = preg_replace('/('.$citiao[$key]['dicname'].')/sui',$citiao[$key]['link_name'],$contents[$i],1);// 只匹配一次关键词
				}
			}

		}
		for ($i=0; $i <$conumber ; $i++) { 
			$s_list[$i]['content']=htmlspecialchars_decode($contents[$i]);
			$str=$s_list[$i]['content'];
			$dbc = array(
				'（' , '）'
			);
			$sbc = array( //半角
				'(', ')'
			);
			$s_list[$i]['content']=str_replace( $sbc, $dbc, $str ); //半角到全角;
		}
		// echo "<pre>";
		// print_r($s_list);  

//        图片缩影
		$i_list=$this->image_model
			->where(array("person_id"=>$person_id,"img_mark"=>1))
			->select();
//        其他资料
		$info_list=$this->information_model
			->where(array("person_id"=>$person_id))
			->select();
		//其他资料图片
		for($j=0;$j<count($info_list);$j++){
			$str=$info_list[$j]['content'];
			$dbc = array(
				'（' , '）'
			);
			$sbc = array( //半角
				'(', ')'
			);
			$info_list[$j]['content']=str_replace( $sbc, $dbc, $str ); //半角到全角;
			$info_imgs=$this->image_model
				->where(array("infor_id"=>$info_list[$j]['id']))
				->select();
			if(count($info_imgs)>0){
				for($i=0;$i<count($info_imgs);$i++){
					$info_list[$j]['img_url'][]=$info_imgs[$i]['img_url'];
				}
			}else{
				$info_list[$j]['img_url']=0;
			}

		}
		$head_list=$this->image_model
			->where(array("person_id"=>$person_id,"img_mark"=>0))
			->select();
		$p_list['img_url']=$head_list[0]['img_url'];
		$new=array();
		$new[0]=$p_list;
		$new[1]=$h_list;
		$new[2]=$s_list;
		$new[3]=$i_list;
		$new[4]=$info_list;
		$aa=isMobile();
		if($aa){
			// $mobile_details=$this->three();	
			// dump($new);
			// // 查出当前人物的二级父类
			$as=$this->term_relationships_model
				 ->where("object_id=$person_id")
				 ->find();
			$term_id=$as['term_id'];
			$af=$this->terms_model
			 		 ->where("term_id=52")
			 		 ->find();
 			$aw=$this->terms_model
 					 ->where("parent=$af[parent]")
 					 ->select();
			 		 // dump($new);
 			// foreach ($new[3] as $key => $value) {
 			// 	$value=[];
 			// 	$value['term_id']=$as['term_id'];
 			// }
 			for($i=0;$i<count($new[3]);$i++){
				if(count($new[3])>0){
					foreach($new[3] as $k=>$v){
						$new[3][$i]['term_id']=$as['term_id'];
						$new[3][$i]['img_num']=$i;
					}
				}
			}
			// echo "<pre>";
 		// 	print_r(json_encode($new));
			$this->assign("new",$new);
			$this->assign("catalogue",$aw);
			$this->display("details_m");
			// echo json_encode($new);

		}else{
			echo json_encode($new);
		}

	}
	public function ajax_list(){
		$id=$_GET['id'];
		$list=$this->person_model
			->where(array('id'=>$id))
			->find();
//        $this->ajaxReturn($list);
		echo json_encode($list);

	}
	public function article(){
		$id=$_POST['id'];
		$data=$this->dictionary_model->where("id=$id")->find();
		$data['content']=htmlspecialchars_decode($data['content']);
		$this->ajaxReturn($data,'json');
	}
	// 手机端文章词条控制器
	public function marticle(){
		$id=I('id');
		$data=$this->dictionary_model->where("id=$id")->find();
		$data['content']=htmlspecialchars_decode($data['content']);
		// $this->ajaxReturn($data,'json');
		// dump($data);
		$where['parent']=array('EQ','0');
        $where['term_id']=array(array('NEQ','72'),array('NEQ','75'));
        $term_list=$this->terms_model
            ->field('name,term_id')
            ->where($where)
            ->order("listorder desc,term_id asc")
            ->select();
        $this->assign("terms",$term_list);
        
		$this->assign("data",$data);
		$this->display("mdic_article");
	}
}